package com.whfc.ms.api.fuum.controller;

import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.AppQualification;
import com.whfc.common.result.Result;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fuum.service.MsQualificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 项目管理-资质管理
 *
 * @author dzg
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/ms/api/qualification")
public class QualificationController {

    @Autowired
    private MsQualificationService qualificationService;

    /**
     * 获取资质列表
     */
    @GetMapping("/list")
    public Result getList(@RequestParam(value = "qualificationName", required = false) String qualificationName,
            @RequestParam(value = "unitId", required = false) Integer unitId) {
        List<AppQualification> list = qualificationService.list(qualificationName, unitId);
        return ResultUtil.success(list);
    }

    /**
     * 获取资质详情
     */
    @GetMapping("/detail/{id}")
    public Result getDetail(@PathVariable("id") Integer id) {
        AppQualification qualification = qualificationService.getById(id);
        return ResultUtil.success(qualification);
    }

    /**
     * 新增资质
     */
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
            @RequestBody AppQualification qualification) {
        // 设置创建人和上传人信息
        qualification.setCreateUser(user.getId());
        qualification.setUpdateUser(user.getId());
        qualification.setUploadUserId(user.getId());
        qualification.setUploadUserName(user.getUsername());
        qualification.setUploadTime(new Date());
        return ResultUtil.success(qualificationService.save(qualification));
    }

    /**
     * 更新资质
     */
    @PostMapping("/update")
    public Result update(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
            @RequestBody AppQualification qualification) {
        // 设置更新人信息
        qualification.setUpdateUser(user.getId());
        return ResultUtil.success(qualificationService.update(qualification));
    }

    /**
     * 删除资质
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean result = qualificationService.delete(id);
        return ResultUtil.success(true);
    }

    /**
     * 批量删除资质
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean result = qualificationService.deleteBatch(ids);
        return ResultUtil.success(true);
    }
}